/*
 * Copyright (c) novafaen design 2009 
 */
package tentaflykt.editor;

import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;

import org.xml.sax.SAXException;

import tentaflykt.editor.gui.EditorTopMenu;
import tentaflykt.editor.gui.Toolbox;
import tentaflykt.editor.xml.EditorSettings;
import tentaflykt.gui.AbstractWindow;
import tentaflykt.util.GBC;
import tentaflykt.util.UserMessage;

/**
 * The main editor window.
 * @author Kristoffer Nilsson
 */
public class Editor
extends AbstractWindow {
  private static final long serialVersionUID = -3484397524634953274L;

  // settings object
  private EditorSettings settings = null;
  
  /*
   * GUI components
   */
  private JDesktopPane workPane = null;
  private Toolbox toolbox = null;
  
  /**
   * Constructor, creates a default <code>EditorWindow</code>.
   */
  public Editor() {
    super(EditorRes.getString("Editor.Title"));
    
    // load editor settings
    loadSettings();
    
    setLocation(settings.getWindowPosX(), settings.getWindowPosY());
    setPreferredSize(new Dimension(
        settings.getWindowWidth(),
        settings.getWindowHeight()));
    setExtendedState(
        settings.getWindowMaximized() ?
            JFrame.MAXIMIZED_BOTH : 
            JFrame.NORMAL);
    
    // initiate window
    initGUI();
    
    pack();
  }
  
  /**
   * Creates and initiates editor window.
   */
  private void initGUI() {
    setLayout(GBC.gbcLayout());

    // setup workspace
    workPane = new JDesktopPane();
    workPane.setBackground(new Color(128, 128, 128));
    
    // override content pane with desktop pane
    setContentPane(workPane);
    
    // create and initiate toolbox
    toolbox = new Toolbox();
    
    toolbox.setLocation(settings.getToolboxPosX(), settings.getToolboxPosY());
    
    workPane.add(toolbox);
    
    // create and add top menu
    setJMenuBar(new EditorTopMenu());
  }
  
  /**
   * Load editor settings from settings/EditorSettings.xml,
   * displays error and load defaults if failed.
   */
  private void loadSettings() {
    try {
      settings = new EditorSettings();
      settings.loadSettings();
    } catch (SAXException ex) {
      // show information, use default values
      UserMessage.showInformationDialog(this, 
          EditorRes.getString("EditorSettings.Information.XMLError"));
    } catch (IOException ex) {
      // show error and information, use default values
      UserMessage.showFatalDialog(this, ex.getMessage());
      UserMessage.showInformationDialog(this, 
          EditorRes.getString("EditorSettings.Information.XMLError"));
    }
  }
  
  @Override
  public void tryExit() {
    UserMessage.showInformationDialog(this, "rly?");
  }

  @Override
  public void onOpen() {
    // no implementation
  }

  @Override
  public void onExit() {
    // window settings
    settings.setWindowWidth(getWidth());
    settings.setWindowHeight(getHeight());
    settings.setWindowPosX(getX());
    settings.setWindowPosY(getY());
    settings.setWindowMaximized(getExtendedState() == JFrame.MAXIMIZED_BOTH);
    
    // toolbox settings
    settings.setToolboxPosX(toolbox.getX());
    settings.setToolboxPosY(toolbox.getY());
    
    try {
      // save settings to file
      settings.save();
    } catch (IOException e) {
      // ignore exception, program is closing. 
      //   handle the possible error when program is launched next time.
    }
  }
}
